using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EVEChatLib;
using EVELogApi;

namespace EVEChat
{
    public partial class Main : Form
    {
        // Create new instances of the classes
        API logApi;
        Lib chatLib = new Lib();
        Timer ti = new Timer();

        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            bool objectCreated = false;
            // Try to use the path in the settings
            while (!objectCreated)
            {
                try
                {
                    logApi = new API(Properties.Settings.Default.logPath);
                }
                catch
                {
                    Error errorBox = new Error("Your log directory is invalid!", "openSettings");
                    errorBox.ShowDialog();
                }

                if (logApi != null)
                {
                    objectCreated = true;
                }
            }

            // Fill the tree
            chatLib.generateChannelsTree(channelTreeview, Properties.Settings.Default.logPath);

            // Now for the main timer
            // Set the timer interval
            ti.Interval = Properties.Settings.Default.channelRefresh;

            // Add the event handler
            this.ti.Tick += new System.EventHandler(this.tiTick);

            // Start it going!
            ti.Start();
        }

        private void tiTick(object sender, EventArgs e)
        {
            // Fill the tree
            chatLib.generateChannelsTree(channelTreeview, Properties.Settings.Default.logPath);
        }

        private void openSettings_Click(object sender, EventArgs e)
        {
            // Make a new settings form
            Settings settingsDialogue = new Settings();

            // Show it as a dialogue
            settingsDialogue.ShowDialog();
        }

        private void openAbout_Click(object sender, EventArgs e)
        {
            // Make a new about form
            About aboutDialogue = new About();

            // Show it as dialogue
            aboutDialogue.ShowDialog();
        }

        private void channelTreeview_DoubleClick(object sender, EventArgs e)
        {
            // They have double clicked a channel!

            // If we aren't loading a parent node
            if (channelTreeview.SelectedNode.Tag.ToString() != " ")
            {
                string selectedChannel = channelTreeview.SelectedNode.Tag.ToString();

                // Open a new one!
                ChatWindow cw = new ChatWindow(selectedChannel);
                cw.TopLevel = false;
                mainSplitContainer.Panel2.Controls.Add(cw);
                cw.Show();
                cw.BringToFront();
            }
        }

        private void arrangeVertical_Click(object sender, EventArgs e)
        {
            chatLib.arrangeVertical(mainSplitContainer.Panel2);
        }

        private void arrangeHorizontal_Click(object sender, EventArgs e)
        {
            chatLib.arrangeHorizontal(mainSplitContainer.Panel2);
        }

        private void arrangeSmart_Click(object sender, EventArgs e)
        {
            chatLib.arrangeSmart(mainSplitContainer.Panel2);
        }

        private void arrangeCascade_Click(object sender, EventArgs e)
        {
            chatLib.arrangeCascade(mainSplitContainer.Panel2);
        }

        private void goFullscreen_Click(object sender, EventArgs e)
        {
            // Toggle fullscreen & icon / text
            chatLib.goFullscreen(this, menuToolstrip);
        }
    }
}